home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / easyproc2.lha / EasyProcess / dragon / Dragon.c next >
Encoding:
C/C++ Source or Header  |  1992-11-05  |  5.6 KB  |  329 lines

  1. #include "Dragon.h"
  2. #include "DragonGad.h"
  3. #include "source:launch/launch.h"
  4.  
  5. struct Window *DragonWin, *InputWin;
  6. struct Screen *Screen;
  7. struct IntuitionBase *IntuitionBase;
  8. struct GfxBase *GfxBase;
  9.  
  10. static char FileName[128];
  11.  
  12. void main (void)
  13. {
  14.     struct IntuiMessage *Message;
  15.     struct Gadget *Gadget;
  16.     LONG Class;
  17.     WORD StartX, StartY;
  18.     struct Dragon Drag, *NewDrag;
  19.  
  20.     SetUp ();
  21.  
  22.     CopyMem (DragonWin->RPort, &Drag.RastPort, sizeof (struct RastPort));
  23.     Drag.DuplicateCount = 0;
  24.     Drag.KindCount = 0;
  25.  
  26.     while (1)
  27.     {
  28.         WaitPort (InputWin->UserPort);
  29.  
  30.         while ( (Message = (struct IntuiMessage *)GetMsg (InputWin->UserPort)) != NULL)
  31.         {
  32.             Class = Message->Class;
  33.             Gadget = (struct Gadget *)Message->IAddress;
  34.             ReplyMsg (Message);
  35.  
  36.             if (Class == CLOSEWINDOW)
  37.             {
  38.                 CleanUp ();
  39.             }
  40.  
  41.             if (Class == GADGETUP)
  42.             {
  43.                 switch (Gadget->GadgetID)
  44.                 {
  45.                     case DAYGAD:
  46.                     {
  47.                         Drag.Day = ReadNumber (DayGadSIBuff, NULL);
  48.                         ActivateGadget (&XGad, InputWin, 0);
  49.                         break;
  50.                     }
  51.  
  52.                     case CELLGAD:
  53.                     {
  54.                         Drag.Cell = ReadNumber (CellGadSIBuff, NULL);
  55.                         ActivateGadget (&NameGad, InputWin, 0);
  56.                         break;
  57.                     }
  58.  
  59.                     case XGAD:
  60.                     {
  61.                         StartX = ReadNumber (XGadSIBuff, NULL);
  62.                         ActivateGadget (&YGad, InputWin, 0);
  63.                         break;
  64.                     }
  65.  
  66.                     case YGAD:
  67.                     {
  68.                         StartY = ReadNumber (YGadSIBuff, NULL);
  69.                         ActivateGadget (&CellGad, InputWin, 0);
  70.                         break;
  71.                     }
  72.  
  73.                     case LOADGAD:
  74.                     {
  75.                         if (Load (&Drag) != 0)
  76.                         {
  77.                             Drag.DuplicateCount = 0;
  78.                             Drag.KindCount = 0;
  79.                         }
  80.                         ActivateGadget (&DayGad, InputWin, 0);
  81.                         break;
  82.                     }
  83.  
  84.                     case GOGAD:
  85.                     {
  86.                         Drag.DragonX = StartX;
  87.                         Drag.DragonY = StartY;
  88.                         NewDrag = AllocMem (sizeof (struct Dragon), 0L);
  89.                         if (NewDrag)
  90.                         {
  91.                             CopyMem (&Drag, NewDrag, sizeof (struct Dragon));
  92.                             if (NULL == StartProcess ("Dragon generator", MakeDragon, NewDrag, -1))
  93.                             {
  94.                                 DisplayBeep (Screen);
  95.                                 FreeMem (NewDrag, sizeof (struct Dragon));
  96.                             }
  97.                         }
  98.                         ActivateGadget (&DayGad, InputWin, 0);
  99.                         break;
  100.                     }
  101.  
  102.                     case STOPGAD:
  103.                     {
  104.                         KillProcesses ();
  105.                         WaitProcesses ();
  106.                         ActivateGadget (&DayGad, InputWin, 0);
  107.                         break;
  108.                     }
  109.  
  110.                     case CLEARGAD:
  111.                     {
  112.                         SetAPen (&Drag.RastPort, 0);
  113.                         RectFill (&Drag.RastPort, 0, 0, 640, 399);
  114.                         SetAPen (&Drag.RastPort, 1);
  115.                         ActivateGadget (&DayGad, InputWin, 0);
  116.                         break;
  117.                     }
  118.  
  119.                     case NAMEGAD:
  120.                     {
  121.                         strcpy (FileName, NameGadSIBuff);
  122.                         ActivateGadget (&DayGad, InputWin, 0);
  123.                         break;
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132. void MakeDragon (struct Dragon *Drag)
  133. {
  134.     Move (&Drag->RastPort, Drag->DragonX, Drag->DragonY);
  135.     Dragon (Drag->Day, Drag->Cell, Drag);
  136.     FreeMem (Drag, sizeof (struct Dragon));
  137. }
  138.  
  139.  
  140. void Dragon (WORD Day, WORD Cell, struct Dragon *Drag)
  141. {
  142.     WORD i;
  143.  
  144.     if (CheckKill ())
  145.     {
  146.         return;
  147.     }
  148.  
  149.     if (Day <= 0)
  150.     {
  151.         Drag->DragonX += Drag->MoveX[Cell];
  152.         Drag->DragonY += Drag->MoveY[Cell];
  153.         Draw (&Drag->RastPort, Drag->DragonX, Drag->DragonY);
  154.     }
  155.     else
  156.     {
  157.         for (i = 0; i < Drag->DuplicateCount; i++)
  158.         {
  159.             Dragon (Day-1, Drag->Duplicate[Cell][i], Drag);
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165. WORD ReadNumber (BYTE *Buffer, WORD *PosPtr)
  166. {
  167.     WORD Number, Sign, Position;
  168.  
  169.     Sign = 1;
  170.     Position = 0;
  171.     Number = 0;
  172.  
  173.     while (*Buffer == ' ' || *Buffer == 10)
  174.     {
  175.         Position++;
  176.         Buffer++;
  177.     }
  178.  
  179.     if (*Buffer == '-')
  180.     {
  181.         Sign = -1;
  182.         Position++;
  183.         Buffer++;
  184.     }
  185.  
  186.     while (*Buffer >= '0' && *Buffer <= '9')
  187.     {
  188.         Number *= 10;
  189.         Number += *Buffer - '0';
  190.         Buffer++;
  191.         Position++;
  192.     }
  193.  
  194.     if (PosPtr != NULL)
  195.     {
  196.         *PosPtr += Position;
  197.     }
  198.  
  199.     return (Sign == 1 ? Number : -Number);
  200. }
  201.  
  202.  
  203. extern void *DOSBase;
  204.  
  205. WORD Load (struct Dragon *Drag)
  206. {
  207.     LONG Len;
  208.     WORD i,j;
  209.     WORD Position;
  210.     BYTE *Buffer;
  211.     BPTR File;
  212.  
  213.     Buffer = NULL;
  214.  
  215.     {
  216.         if ( (File = Open (FileName, MODE_OLDFILE)) == NULL)
  217.             goto bad;
  218.  
  219.         Len = Seek (File, 0, OFFSET_END);
  220.         Len = Seek (File, 0, OFFSET_BEGINNING);
  221.  
  222.         if ( (Buffer = AllocMem (Len, MEMF_CLEAR)) == NULL)
  223.         {
  224.             Close (File);
  225.             goto bad;
  226.         }
  227.  
  228.         Read (File, Buffer, Len);
  229.         Close (File);
  230.  
  231.         Position = 0;
  232.  
  233.         Drag->KindCount = ReadNumber (Buffer+Position, &Position);
  234.         Drag->DuplicateCount = ReadNumber (Buffer+Position, &Position);
  235.  
  236.         if (Drag->KindCount <= 0 || Drag->DuplicateCount <= 0)
  237.             goto bad;
  238.  
  239.         for (i = 0; i < Drag->KindCount; i++)
  240.         {
  241.             Drag->MoveX[i] = ReadNumber (Buffer+Position, &Position);
  242.         }
  243.  
  244.         for (i = 0; i < Drag->KindCount; i++)
  245.         {
  246.             Drag->MoveY[i] = ReadNumber (Buffer+Position, &Position);
  247.         }
  248.  
  249.         for (i = 0; i < Drag->KindCount; i++)
  250.         {
  251.             for (j = 0; j < Drag->DuplicateCount; j++)
  252.             {
  253.                 Drag->Duplicate[i][j] = ReadNumber (Buffer+Position, &Position);
  254.                 if (Drag->Duplicate[i][j] < 0)
  255.                 {
  256.                     goto bad;
  257.                 }
  258.             }
  259.         }
  260.  
  261.         FreeMem (Buffer, Len);
  262.         return 0;
  263.     }
  264.  
  265. bad:
  266.     if (Buffer)
  267.         FreeMem (Buffer, Len);
  268.     return 1;
  269. }
  270.  
  271.  
  272. void SetUp (void)
  273. {
  274.     if ( (GfxBase = (void *)OpenLibrary ("graphics.library", 0)) == NULL)
  275.     {
  276.         CleanUp ();
  277.     }
  278.  
  279.     if ( (IntuitionBase = (void *)OpenLibrary ("intuition.library", 0)) == NULL)
  280.     {
  281.         CleanUp ();
  282.     }
  283.  
  284.     if ( (Screen = OpenScreen(&DragonNewScreen)) == NULL )
  285.     {
  286.         CleanUp();
  287.     }
  288.  
  289.     SetRGB4 (&Screen->ViewPort, 0, 0, 0, 0);
  290.     SetRGB4 (&Screen->ViewPort, 1, 4, 6, 14);
  291.  
  292.     DragonNewWin.Screen = InputNewWin.Screen = Screen;
  293.  
  294.     if ( (DragonWin = OpenWindow (&DragonNewWin)) == NULL )
  295.     {
  296.         CleanUp ();
  297.     }
  298.  
  299.     if ( (InputWin = OpenWindow (&InputNewWin)) == NULL )
  300.     {
  301.         CleanUp ();
  302.     }
  303.  
  304. }
  305.  
  306.  
  307. void CleanUp (void)
  308. {
  309.     KillProcesses ();
  310.     WaitProcesses ();
  311.  
  312.     if (InputWin)
  313.         CloseWindow (InputWin);
  314.  
  315.     if (DragonWin)
  316.         CloseWindow (DragonWin);
  317.  
  318.     if (Screen)
  319.         CloseScreen (Screen);
  320.  
  321.     if (IntuitionBase)
  322.         CloseLibrary (IntuitionBase);
  323.  
  324.     if (GfxBase)
  325.         CloseLibrary (GfxBase);
  326.  
  327.     exit (0);
  328. }
  329.